package com.vitabuybuy.tv.dialog

import android.content.Context
import android.location.Location
import android.view.View
import android.view.animation.Animation
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.library.common.ext.logE
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.GoodsPopupScreenBinding
import com.vitabuybuy.tv.dto.BrandDto
import com.vitabuybuy.tv.dto.SalePriceDto
import com.vitabuybuy.tv.utils.GetAddressUtil
import com.vitabuybuy.tv.utils.LocationUtil
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig

/**
 * 筛选弹出框
 */
class GoodsScreenPopup(
    context: Context,
    bradList: MutableList<BrandDto>?,
    salePrice: SalePriceDto
) :
    BasePopupWindow(context) {
    private lateinit var brandListAdapter: BrandListAdapter
    private lateinit var mBinding: GoodsPopupScreenBinding
    private var regionId = -1

    init {
        bradList?.run {
            brandListAdapter.addData(this)
        }
        salePrice.apply {
            if (start != null) {
                mBinding.etLowPrice.setText(if (start == 0.0) "" else start.toString())
            }
            if (end != null) {
                mBinding.etHighPrice.setText(if (end == 0.0) "" else end.toString())
            }
        }
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.goods_popup_screen)
    }

    fun setRegion(regionId: Int, regionStr: String) {
        this.regionId = regionId
        mBinding.tvAddress.text = regionStr
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        mBinding = DataBindingUtil.bind<ViewDataBinding>(contentView) as GoodsPopupScreenBinding
        LocationUtil.getCurrentLocation(context, object : LocationUtil.LocationCallBack {
            override fun onSuccess(location: Location) {
                val address = GetAddressUtil(context).getAddress(
                    location.longitude,
                    location.latitude
                )
                if (!address.isNullOrEmpty()) {
                    val add = address[0]
                    mBinding.tvAddress.text = add.countryName
                }
            }

            override fun onFail(msg: String) {
                msg.logE()
            }
        })
        setViewClickListener(View.OnClickListener {
            if (mBinding.rcvBrand.visibility == View.VISIBLE) {
                mBinding.rcvBrand.visibility = View.GONE
                mBinding.ivBrandSwitch.setImageResource(R.drawable.icon_common_arrow_down)
            } else {
                mBinding.rcvBrand.visibility = View.VISIBLE
                mBinding.ivBrandSwitch.setImageResource(R.drawable.icon_common_arrow_up)
            }
        }, mBinding.ivBrandSwitch)
        setViewClickListener(View.OnClickListener {
            if (mBinding.llPriceRange.visibility == View.VISIBLE) {
                mBinding.llPriceRange.visibility = View.GONE
                mBinding.ivPriceRangeSwitch.setImageResource(R.drawable.icon_common_arrow_down)
            } else {
                mBinding.llPriceRange.visibility = View.VISIBLE
                mBinding.ivPriceRangeSwitch.setImageResource(R.drawable.icon_common_arrow_up)
            }
        }, mBinding.ivPriceRangeSwitch)
        setViewClickListener(View.OnClickListener {
            if (mBinding.llDeliveryFrom.visibility == View.VISIBLE) {
                mBinding.llDeliveryFrom.visibility = View.GONE
                mBinding.ivDeliveryFromSwitch.setImageResource(R.drawable.icon_common_arrow_down)
            } else {
                mBinding.llDeliveryFrom.visibility = View.VISIBLE
                mBinding.ivDeliveryFromSwitch.setImageResource(R.drawable.icon_common_arrow_up)
            }
        }, mBinding.ivDeliveryFromSwitch)
        setViewClickListener(View.OnClickListener {
            brandListAdapter.apply {
                for (brand in mData) {
                    brand.isChoose = false
                }
                notifyDataSetChanged()
            }
            mBinding.etLowPrice.setText("")
            mBinding.etHighPrice.setText("")
        }, mBinding.btnReset)
        setViewClickListener(View.OnClickListener {
            onConfirm.invoke(
                brandListAdapter.mData,
                SalePriceDto().apply {
                    if (!mBinding.etLowPrice.text.isNullOrEmpty()) {
                        start = mBinding.etLowPrice.text.toString().toDouble()
                    }
                    if (!mBinding.etHighPrice.text.isNullOrEmpty()) {
                        end = mBinding.etHighPrice.text.toString().toDouble()
                    }
                    if (start != null && end != null && start > end) {
                        ToastUtils.showShort(context.getString(R.string.price_range_tip))
                        return@OnClickListener
                    }
                }, regionId
            )
            this.dismiss()
        }, mBinding.btnConfirm)
        setViewClickListener(View.OnClickListener {
            changeLocation.invoke()
        }, mBinding.tvChange, mBinding.tvAddress)
        brandListAdapter = BrandListAdapter().apply {
            setOnItemClickListener { _, _, position ->
                mData[position].isChoose = !mData[position].isChoose
                notifyDataSetChanged()
            }
        }
        mBinding.rcvBrand.apply {
            adapter = brandListAdapter
            layoutManager = GridLayoutManager(context, 3)
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.FROM_RIGHT)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.TO_RIGHT)
            .toDismiss()
    }

    lateinit var onConfirm: (bradList: MutableList<BrandDto>, salePrice: SalePriceDto, regionId: Int) -> Unit
    lateinit var changeLocation: () -> Unit
}